import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import { useRouter} from 'vue-router'
import { ElMessage } from 'element-plus'
const router = useRouter()
const instance = axios.create({
    baseURL:"http://kumanxuan1.f3322.net:8881/cms",
    timeout:10000
    
})

instance.interceptors.request.use((config:AxiosRequestConfig<unknown>)=>{
    // if(!config.url.endsWith("/login") && !config.url.endsWith("/captchaImage")){
    //     config.headers['Authorization'] ='Bearer '+localStorage.getItem("edn-authorization-token")
    // }
    return config
},err=>{
    return Promise.reject(err)
})

instance.interceptors.response.use((res)=>{
    let res_data = res.data;
    // if(res_data.code!=200){
    //     ElMessage.error(res_data.msg || "网络请求错误")
    //     if(res_data.code==401){
    //         localStorage.removeItem("edn-authorization-token");
    //         router.push("/login")
    //     }
    //     return false
    // }
     if(res_data.code !=0){
        ElMessage.error(res_data.msg || "网络请求错误")
        return false;
     }
    return res.data 
},err=>{
    return Promise.reject(err)
})

export default instance;